import { mapGetters } from 'vuex';
const formatRouteTitle = (to, from, next) => {
	let topicId = to.query.id;
	let title = to.meta.title || '';
	to.meta.title = `${!topicId ? '新增' : '编辑'}${title}`;
	next();
};
export default {
	beforeRouteEnter: formatRouteTitle,
	beforeRouteUpdate: formatRouteTitle,
	data() {
		return {}
	},
	computed: {
		...mapGetters(['userInfo', 'siteConfig', 'dictEnums', 'principalData']),
		formId() {
			return this.$route.query.id;
		},
		addOrEdit() {
			return !this.formId;
		},
	},
	mounted() {
		this.initFormData();
	},
	methods: {
		//表单初始化
		async initFormData() {
			this.$refs['form'].resetFields();
			if (this.addOrEdit) {
				this.formData = { piList: [], collaList: [], fundsList: [], measureList: [] };
			} else {
				this.formData = await this.getFormDetailData(this.formId);
			}
			if (!!this.formatFormParams) {
				this.formatFormParams(this.formData);
			}
		},

		//获取表单详情
		async getFormDetailData(id) {
			let result = await this.detailFunction(id).then(res => res.data);
			if (result.code === '0') {
				return Promise.resolve(this.$detailData(result.data));
			}
		},

		//下载模板
		handleDownloadTemp(type) {
			window.location.href = `${window.baseUrl}tbTopic/down/${type}`;
		},

		returnStatusFormatter(row) {
			let attrs = { size: 'medium' };
			return this.$createElement('elTag', { attrs }, '待提交');
		}
	}
}